package com.lgz.dbcourse.gui;

import com.lgz.dbcourse.dto.ResultSet;
import com.lgz.dbcourse.dto.StaffInputDTO;
import com.lgz.dbcourse.dto.StaffOutputDTO;
import com.lgz.dbcourse.service.StaffService;
import com.lgz.dbcourse.service.impl.StaffServiceImpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * @Author: LGZ
 * @Date: 2021/4/13 15:44
 * @Description:
 */
public class LoginPanel extends JPanel {
    private static final int WIDTH = 200;
    private static final String LOGIN = "Login";
    private static final String BACK = "Back";
    private static JTextField username;
    private static JPasswordField password;
    private static LoginPanel loginPanel;
    private static StaffFrame jFrame;
    private JButton login;

    public LoginPanel(StaffFrame jf) {
        jFrame = jf;
        this.setLayout(new GridLayout(4, 1));
//        this.setSize(WIDTH, 150);
        this.setBounds(380, 180, WIDTH, 150);
        loginPanel = this;
        createTitle();
        createUsername();
        createPassword();
        createButton();


    }

    public void createTitle() {
        JLabel loginTitle = new JLabel("登录");
        loginTitle.setHorizontalAlignment(JLabel.CENTER);
        loginTitle.setFont(new Font("楷体", Font.PLAIN, 20));
        this.add(loginTitle);
    }

    public void createUsername() {
        JPanel jPanel = new JPanel(null);
        JLabel jLabel = new JLabel("账号：");
        jLabel.setBounds(0, 0, 40, 30);
        username = new JTextField();
        username.setBounds(40, 0, 160, 30);
        jPanel.add(jLabel);
        jPanel.add(username);
        username.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyChar() == KeyEvent.VK_ENTER) {
                    login.doClick();
                }
            }
        });
        this.add(jPanel);
    }

    public void createPassword() {
        JPanel jPanel = new JPanel(null);
        JLabel jLabel = new JLabel("密码：");
        jLabel.setBounds(0, 0, 40, 30);
        password = new JPasswordField();
        password.setBounds(40, 0, 160, 30);
        jPanel.add(jLabel);
        jPanel.add(password);
        password.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyChar() == KeyEvent.VK_ENTER) {
                    login.doClick();
                }
            }
        });
        this.add(jPanel);
    }

    public void createButton() {
        JPanel jPanel = new JPanel(null);
        login = new JButton("登录");
        login.setActionCommand(LOGIN);
        login.setBounds(40, 0, 70, 30);
        JButton back = new JButton("返回");
        back.setActionCommand(BACK);
        back.setBounds(130, 0, 70, 30);
        jPanel.add(login);
        jPanel.add(back);
        back.addActionListener(new ButtonAction());
        login.addActionListener(new ButtonAction());
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyChar() == KeyEvent.VK_ENTER) {
                    login.doClick();
                }
            }
        });
        this.add(jPanel);
    }

    private static class ButtonAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (LOGIN.equals(e.getActionCommand())) {
                // 登录

                StaffService staffService = new StaffServiceImpl();
                StaffInputDTO staffInputDTO = new StaffInputDTO();

//                staffInputDTO.setUsername("staff001");
//                staffInputDTO.setPassword("123456");
                staffInputDTO.setUsername(username.getText());
                staffInputDTO.setPassword(String.valueOf(password.getPassword()));
                ResultSet resultSet = staffService.login(staffInputDTO);


                if (200 == resultSet.getCode()) {
                    jFrame.remove(loginPanel);
                    jFrame.add(new ManagePanel(jFrame, (StaffOutputDTO)resultSet.getData()));
                jFrame.repaint();
                jFrame.validate();
                } else {
                    // 用户名与密码不匹配
                    JLabel jLabel = new JLabel(resultSet.getMsg());
                    jLabel.setHorizontalAlignment(JLabel.CENTER);
                    JDialog jDialog = new JDialog(jFrame, "Error");
                    jDialog.setSize(200, 100);
                    jDialog.add(jLabel);
                    jDialog.setResizable(false);
                    jDialog.setLocationRelativeTo(jFrame);
                    jDialog.setModal(true);
                    jDialog.setVisible(true);
                }
            } else if (BACK.equals(e.getActionCommand())) {
                // 返回主界面
                jFrame.dispose();
                new MainFrame().start();
            }
        }
    }
}
